package misc;
import interesado.Interesado_GUI;
import interesado.Registrarse_GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import empleadoAdministrativo.Empleado_GUI;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class Login_GUI extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel lblUsuario;
	private JTextField txtUsuario;
	private JButton btnLogin;
	private JPasswordField txtPassword;
	private JLabel lblPassword;
	
	
	//Agregados
	private Interesado_GUI interesado;
	private JButton btnRegistrarse;
	private Empleado_GUI empleado;
	private Logica logica;
	private Registrarse_GUI registrarse;
	
	
	/**
	* Auto-generated main method to display this JFrame
	*/
	
	
	public Login_GUI() {
		super();
		logica = new Logica();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setTitle("Login");
			setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				lblUsuario = new JLabel();
				getContentPane().add(lblUsuario);
				lblUsuario.setText("Usuario:");
				lblUsuario.setBounds(50, 34, 108, 21);
				lblUsuario.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lblPassword = new JLabel();
				getContentPane().add(lblPassword);
				lblPassword.setText("Password:");
				lblPassword.setBounds(50, 68, 108, 22);
				lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				txtUsuario = new JTextField();
				getContentPane().add(txtUsuario);
				txtUsuario.setBounds(170, 33, 121, 23);
			}
			{
				txtPassword = new JPasswordField();
				getContentPane().add(txtPassword);
				txtPassword.setBounds(170, 68, 121, 23);
				
			}
			{
				btnLogin = new JButton();
				getContentPane().add(btnLogin);
				btnLogin.setText("Login");
				btnLogin.setBounds(243, 125, 83, 23);
				btnLogin.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						//Recupero los datos
						String usuario=txtUsuario.getText();
						String password=txtPassword.getText();
						login(usuario,password);
					}
				});
			}
			{
				btnRegistrarse = new JButton();
				getContentPane().add(btnRegistrarse);
				btnRegistrarse.setText("Registrarse");
				btnRegistrarse.setBounds(118, 125, 103, 23);
				btnRegistrarse.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						btnRegistrarseActionPerformed(evt);
					}
				});
			}
			pack();
			this.setSize(412, 228);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

	
	
	
	//Metodos agregados
	
	
	public void limpiarCampos() {
		txtPassword.setText("");
		txtUsuario.setText("");
		txtUsuario.requestFocus();
	}
	
	private void login(String usuario, String password){
		//Valido los datos (s/DD)
		if(usuario.equals("") || password.equals("")){
			
			JOptionPane.showMessageDialog(null, "Debe completar los datos faltantes","Error",JOptionPane.ERROR_MESSAGE);		
		}
		else {
				if (password.length()>20) 
					JOptionPane.showMessageDialog(null, "El campo password no debe superar los 20 caracteres","Error",JOptionPane.ERROR_MESSAGE);
				else
				{
						//Controlo los datos
						int res=logica.controlarUsuario(usuario,password);
						
					
						//Muestro interfaz correspondiente
						if(res==-1)
							 {
							JOptionPane.showMessageDialog(null, "El usuario ingresado no existe","Error",JOptionPane.ERROR_MESSAGE);
							txtPassword.setText("");
							txtUsuario.setText("");
							 }
						
						else if(res==0){//Muestro pantalla EA
							{
								empleado = new Empleado_GUI(this,usuario,password,logica);				
								empleado.setLocationRelativeTo(this);
								empleado.setVisible(true);
								System.out.println("Empleado Administrativo");
							}
							
							
							//Oculto pantalla actual
							this.setVisible(false);
						}		
						else{//muestro pantalla Interesado
							
							interesado= new Interesado_GUI(this,logica,usuario,password);				
							
							interesado.setLocationRelativeTo(this);
							String [] datos = logica.getDatos();
							interesado.setVisible(true);
							interesado.setTitle("Usuario: "+datos[0]+", "+datos[1]+" "+" - LU: "+usuario);
							
							
							//Oculto pantalla actual
							this.setVisible(false);
						}
				
				}
				
		}	
	}
	
	private void btnRegistrarseActionPerformed(ActionEvent evt) 
	{
		registrarse = new Registrarse_GUI(this,logica);
		registrarse.setLocationRelativeTo(this);
		this.limpiarCampos();
		this.setVisible(false);	
	}
}
